﻿using Appliaction.Dto.Inv;
using Appliaction.IAppServices.Inv;
using AutoMapper;
using Domain.Entitys.Inv;
using Domain.IRepositorys.Inv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Inv
{
    /// <summary>
    /// 库存 调拨 接口实现类
    /// </summary>
    public class Inv_TrfServices : IInv_Trfservices
    {
        private readonly IInv_TrfRepository  inv_TrfRepository;

        private readonly IMapper mapper;

        public Inv_TrfServices(IInv_TrfRepository inv_TrfRepository, IMapper mapper)
        {
            this.inv_TrfRepository = inv_TrfRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 根据单据编号查询库存调拨单id信息,来添加详细信息
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> inv_tfr_id(string trf_code)
        {
            return inv_TrfRepository.inv_tfr_id(trf_code);
        }

        /// <summary>
        /// 添加库存调拨
        /// </summary>
        /// <param name="inv_TrfAddDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_trf_Insert(Inv_TrfAddDto inv_TrfAddDto)
        {
            Inv_trf inv = mapper.Map<Inv_trf>(inv_TrfAddDto);
            return inv_TrfRepository.Inv_trf_Insert(inv);
        }

        /// <summary>
        /// 根据编号查询调拨单信息
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<Inv_trf>> Inv_trf_SelectCode(string trf_code)
        {
            return inv_TrfRepository.Inv_trf_SelectCode(trf_code);
        }

        /// <summary>
        /// 库存移转查询总数
        /// </summary>
        /// <param name="trf_code"></param>
        /// <param name="trf_type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_trf_selectCount(string trf_code, string trf_type, string proName)
        {
          return inv_TrfRepository.Inv_trf_selectCount(trf_code, trf_type,proName);
        }
        /// <summary>
        /// 根据编码获取调拨单详情
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<Inv_trfproddtl>> Inv_trf_SelectDetail(string trf_code)
        {
            return inv_TrfRepository.Inv_trf_SelectDetail(trf_code);
        }

        /// <summary>
        /// 库存调拨查询分页
        /// </summary>
        /// <param name="trf_code"></param>
        /// <param name="trf_type"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Trf_selectDto> Inv_trf_selectFY(string trf_code, string trf_type, int currentPage, int pageSize, string proName)
        {
            return inv_TrfRepository.Inv_trf_selectFY(trf_code, trf_type, currentPage, pageSize,proName);
        }
        /// <summary>
        /// 调拨添加详情表
        /// </summary>
        /// <param name="inv_TrftrfproddtlAddDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Inv_trf_Selecttrfproddtl(List<Inv_TrftrfproddtlAddDto> inv_TrftrfproddtlAddDto)
        {
            var entities = inv_TrftrfproddtlAddDto.Select(dto => new Inv_trfproddtl
            {
                Trf_id = dto.Trf_id,
                Pro_name = dto.Pro_name,
                Pro_code = dto.Pro_code,
                Pro_brand = dto.Pro_brand,
                Pro_model = dto.Pro_model,
                Unitmeas_id = dto.Unitmeas_id,
                Inventory_numder = dto.Inventory_numder,
                Pro_cost_price = dto.Pro_cost_price,
                Pro_sale_price = dto.Pro_sale_price,
                stock_entry_no= dto.stock_entry_no,
                Pending_numder=dto.Pending_numder
            }).ToList();

            return await inv_TrfRepository.Inv_trf_Selecttrfproddtl(entities);
        }



        /// <summary>
        /// 修改调拨审核状态
        /// </summary>
        /// <param name="inv_Trf_AuditUpdadteaDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Trf_examineUpdate(Inv_Trf_AuditUpdadteaDto inv_Trf_AuditUpdadteaDto)
        {
          Inv_trf inv_Trf=mapper.Map<Inv_trf>(inv_Trf_AuditUpdadteaDto);
           return inv_TrfRepository.Inv_Trf_examineUpdate(inv_Trf);
        }
        /// <summary>
        /// 删除调拨单
        /// </summary>
        /// <param name="Trf_id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Trf_delete(int Trf_id)
        {
            return inv_TrfRepository.Inv_Trf_delete(Trf_id);
        }
        /// <summary>
        /// 修改库存调拨单和同一时间执行添加修改库存调拨单详情
        /// </summary>
        /// <param name="inv_TrfUpdateMerage"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_othwr_Update(Inv_TrfUpdateMerage inv_TrfUpdateMerage)
        {
           Inv_trf inv_Trf=mapper.Map<Inv_trf>(inv_TrfUpdateMerage.UpdateDto);
           return inv_TrfRepository.Inv_othwr_Update(inv_Trf, inv_TrfUpdateMerage.UpdateDtls);
        }

        /// <summary>
        /// 其他入库单入库查询调拨单
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Inv_trf> Inv_Select_MainSelect(string code, int currentPage, int pageSize)
        {
            return inv_TrfRepository.Inv_Select_MainSelect(code,currentPage, pageSize);
        }
        /// <summary>
        /// 其他入库单入库查询调拨单--总数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_Select_MainSelect_Count(string code)
        {
            return inv_TrfRepository.Inv_Select_MainSelect_Count(code);
        }
        /// <summary>
        /// 回调库存调拨单的数量--其他入库单审核入库之后,减少调拨单的数量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //public Task<int> Inv_Trf_update_numderl(List<Inv_TrfupdateNumderDto> inv_TrfupdateNumderDto) { 
        //var entis= inv_TrfupdateNumderDto.Select(dto=> new Inv_trfproddtl { 
        // Pro_code=dto.Pro_code,
        // stock_entry_no=dto.stock_entry_no,
        // Inventory_numder=dto.Inventory_numder
        //}).ToList();
        //return inv_TrfRepository.Inv_Trf_update_numderl(entis);
        //}


        /// <summary>
        /// 查询调拨单--出库单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Inv_trf> Inv_Select_othoutordSelect(string code, int currentPage, int pageSize) { 
        
            return inv_TrfRepository.Inv_Select_othoutordSelect(code, currentPage, pageSize);
        }

        /// <summary>
        /// 其他出库单入库查询调拨单
        /// </summary>
        /// <returns></returns>
        public Task<int> Inv_Select_othoutordSelect_Count(string code) {

            return inv_TrfRepository.Inv_Select_othoutordSelect_Count(code);
        }


        /// <summary>
        /// 根据其他出库单查询的Id,查询出来关联单号,去修改调拨单的字段状态
        /// </summary>
        /// <param name="othoutord_id"></param>
        /// <returns></returns>
        public Task<int> Inv_Trf_update_trf_ckpd(int othoutord_id) { 
        
         return inv_TrfRepository.Inv_Trf_update_trf_ckpd(othoutord_id);
        }

        /// <summary>
        /// 入库完善审核后,修改调拨单的待入库数量
        /// </summary>
        /// <param name="inv_Othwrproddtls"></param>
        /// <returns></returns>
        public Task<int> Inv_Trf_update_Pending_numder(List<Inv_Trf_update_Pending_numderUpdateDto> inv_Trf_update_Pending_numderUpdateDto) { 
        
   

        return inv_TrfRepository.Inv_Trf_update_Pending_numder(inv_Trf_update_Pending_numderUpdateDto);
        }
    }
}
